//
//  JMOrderInfoVC.m
//  NewiOSProject
//
//  Created by JMMac on 15.1.24.
//  Copyright © 2024 范智渊. All rights reserved.
#import "JMOrderInfoVC.h"
#import "JMOrderInfoModel.h"
#import "JMOrderInfoCell.h"
#import "JMRepayVC.h"
#import "JMOrderHeaderV.h"
#import "JMExtenDetailVC.h"

@interface JMOrderInfoVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)JMOrderHeaderV *headView;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)UIView *bottomView;
@property (nonatomic, strong)NSMutableArray *dataList;
@property (nonatomic, strong)JMOrderInfoModel *model;
@end

@implementation JMOrderInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.TitleLabel.text = @"Detalles del préstamo";
    self.TitleLabel.textColor = WhiteColor();
    self.NavImg.backgroundColor = mainSchemeColor();
    if (self.listModel.status.intValue==180) {
        self.NavImg.backgroundColor = Hex_Str_COLOR(@"#BD4232");
    }
    [JMCommonMethod uploadStatistics:@"LoanStatus" actionType:@"Enter" parameters:@{@"type":self.listModel.stateTypeString}];

    [self getDataList];
}

-(void)getDataList{
    [self startAnimating: nil];
    NSDictionary *param = @{@"orderNo":self.listModel.orderNo,@"pageType":self.listModel.pageType};
    
    [[JMBaseRequest sharedInstance] requestBasePost:BasePostRequestUrl(@"/api/order/order-detail-base64-flexible") params:param success:^(NSDictionary * _Nullable dataDic, NSArray * _Nullable dataArray) {
        [self stopAnimating];
        self.model = [JMOrderInfoModel mj_objectWithKeyValues:dataDic];
        NSMutableArray *arr = @[].mutableCopy;
        [arr addObject:@{@"key":@"Fecha de vencimiento",@"value":self.model.expiryTime}];
        for (NSDictionary *dict in self.model.pairs) {
            [arr addObject:dict];
        }
        self.model.pairs = arr;
        self.model.listModel = self.listModel;
        [self.view addSubview:self.tableView];
        [self.tableView reloadData];
        [self.view addSubview:self.bottomView];

    } fail:^(NSString * _Nonnull errorMsg) {
        [self stopAnimating];
    }];
    
}

//MARK: -UITableViewDataSource UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.model.pairs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JMOrderInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JMOrderInfoCell" forIndexPath:indexPath];
    cell.selectionStyle = 0;
    if (indexPath.row < self.model.pairs.count) {
        NSDictionary*dict = self.model.pairs[indexPath.row];
        cell.model = dict;
    }
    return cell;
}

//MARK: -lazyLoad
-(JMOrderHeaderV *)headView{
    if (!_headView) {
        _headView = [[JMOrderHeaderV alloc]initWithFrame:(CGRectMake(0, 0, kAppW(), 1))];
        _headView.infoModel = self.model;
    }
    return _headView;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,NAVIGATION_BAR_HEIGHT(), kAppW(), kAppH()) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.bounces = NO;
        _tableView.rowHeight = 52;
        _tableView.dataSource = self;
        [_tableView registerClass:[JMOrderInfoCell class] forCellReuseIdentifier:@"JMOrderInfoCell"]; // 注册UITableViewCell
        _tableView.separatorColor = mainSepartorColor();
        _tableView.sectionHeaderTopPadding = 0;
        _tableView.backgroundColor = Hex_Str_COLOR(@"#fffff");
        [_tableView setTableHeaderView:self.headView];
        _tableView.sectionHeaderTopPadding = 0;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = false;
        }
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    return _tableView;
}
-(UIView *)bottomView{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]initWithFrame:(CGRectMake(0, 0, kAppW(), 10))];
        UIView *contV = [[UIView alloc]initWithFrame:(CGRectMake(0, 16, kAppW(), 10))];
        contV.backgroundColor = WhiteColor();
        [_bottomView addSubview:contV];

        if (_listModel.status.intValue==200) {
           UIButton * contractBtn = [JMControl createButtonWithFrame:(CGRectMake(0,+12, kAppW(), 40)) Target:self Action:@selector(contractBtnClick) Title:@"Ver contrato de préstamo>" titlColor:Hex_Str_COLOR(@"#3C57F8") font:Font(12) tag:1 backgroundColor:nil];
            [contV addSubview:contractBtn];
            [contV addSubview:self.footerView];
            self.footerView.top = contractBtn.bottom+16;
            contV.height = self.footerView.bottom+24+JF_BOTTOM_SPACE();
            _bottomView.height = contV.bottom;
        }else
        if (_listModel.status.intValue==180 || _listModel.status.intValue==170) {
            //逾期还款
            UIButton * delayBtn = [[JMLoadingButton  alloc]initWithFrame:(CGRectMake(24, 20, kAppW()-48, 56))title:@"Extender plazo" font:mediumFont(17) titleLabelColor:Hex_Str_COLOR(@"#FEFFCC") backgoundColor:mainGradientColor(CGSizeMake(kAppW()-48, 56)) action:@selector(delayBtnClick) target:self];
             [contV addSubview:delayBtn];
             delayBtn.cornerRadius = 28;
            
            //正常还款
            UIButton * repaymentBtn = [[JMLoadingButton  alloc]initWithFrame:(CGRectMake(delayBtn.left, delayBtn.bottom+20, kAppW()-delayBtn.left*2, 56))title:@"Ir a reembolsar" font:Font(16) titleLabelColor:Hex_Str_COLOR(@"#3C57F8") backgoundColor:UIColor.clearColor action:@selector(repaymentBtnBtnClick) target:self];
            repaymentBtn.layer.borderColor = Hex_Str_COLOR(@"#3C57F8").CGColor;
            repaymentBtn.layer.borderWidth = 1;
            repaymentBtn.cornerRadius = 28;
            [contV addSubview:repaymentBtn];
            
            [contV addSubview:self.footerView];
            self.footerView.top = repaymentBtn.bottom+24;
            contV.height = repaymentBtn.bottom+24+60+JF_BOTTOM_SPACE();
            _bottomView.height = contV.bottom;
            contV.layer.shadowColor = HexAlpha_Str_COLOR(@"#000000", 0.1).CGColor;
            contV.layer.shadowOpacity = 1;
            contV.layer.shadowRadius = 16;
        }else{
            [contV addSubview:self.footerView];
            contV.height = self.footerView.bottom+24+JF_BOTTOM_SPACE();
            _bottomView.height = contV.bottom;
        }
        _bottomView.top = kAppH()-_bottomView.height;
        _tableView.height = _bottomView.top-NAVIGATION_BAR_HEIGHT();
    }
    return _bottomView;
}

//展期明细
-(void)delayBtnClick{

    JMExtenDetailVC*vc = JMExtenDetailVC.new;
    vc.infoModel = self.model;
    [self.navigationController pushViewController:vc animated:YES];
}
//正常还款页面
-(void)repaymentBtnBtnClick{
    
    [JMCommonMethod uploadStatistics:@"TotalRepaymentMethod" actionType:@"Enter" parameters:@{@"loanOrderId":self.listModel.orderNo}];
    JMRepayVC *vc = JMRepayVC.new;
    vc.infoModel = self.model;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)contractBtnClick{
    
}
@end
